﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using QualityRpt.Service;
using System.Data;

namespace QualityRPT
{
    public partial class StatRawData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void ButtonQ_Click(object sender, EventArgs e)
        {
            DateTime startTime = DateTime.Now.AddMonths(-1);
            DateTime endTime = DateTime.Now;
            if (textBoxStart.Text.Trim() != string.Empty)
            {
                startTime = DateTime.Parse(textBoxStart.Text);
                endTime = DateTime.Parse(textBoxEnd.Text);
            }
            DataSet resultSet  = TransNoteService.StatRawData(startTime, endTime);
            
            //分析
            DataTable entry_zh_anTable = resultSet.Tables[0].Clone();
            DataTable entry_special_anTable = resultSet.Tables[1].Clone();
            DataTable exit_zh_anTable = resultSet.Tables[2].Clone();
            DataTable exit_special_anTable = resultSet.Tables[3].Clone();
            DateTime anStartTime = startTime.Date;
            endTime = endTime.Date;
            while (anStartTime < endTime)
            {
                DataRow[] entry_zh_rows = resultSet.Tables[0].Select("sum_date='"+anStartTime.ToString("yyyy-MM-dd")+"'");
                DataRow[] entry_special_rows = resultSet.Tables[1].Select("sum_date='" + anStartTime.ToString("yyyy-MM-dd") + "'");
                DataRow[] exit_zh_rows = resultSet.Tables[2].Select("sum_date='" + anStartTime.ToString("yyyy-MM-dd") + "'");
                DataRow[] exit_speical_rows = resultSet.Tables[3].Select("sum_date='" + anStartTime.ToString("yyyy-MM-dd") + "'");
                //入口成功
                if (entry_zh_rows.Length > 0)
                {
                    bool normal = false;
                    object curSumcnt = entry_zh_rows[0]["trans_count"];
                    if (curSumcnt != DBNull.Value)
                    {
                        if (Convert.ToInt32(curSumcnt) >= dayMin && Convert.ToInt32(curSumcnt) <= dayMax)
                        {
                            normal = true;
                        }
                    }
                    if (!normal)
                    {
                        entry_zh_anTable.ImportRow(entry_zh_rows[0]);
                    }
                }
                else
                {
                    DataRow addRow = entry_zh_anTable.NewRow();
                    addRow["sum_date"] = anStartTime; addRow["trans_count"] = 0;
                    entry_zh_anTable.Rows.Add(addRow);
                    //明细上也加上
                    DataRow addRsRow = resultSet.Tables[0].NewRow();
                    addRsRow["sum_date"] = anStartTime; addRsRow["trans_count"] = 0;
                    resultSet.Tables[0].Rows.Add(addRsRow);
                }
                //入口失败
                if (entry_special_rows.Length > 0)
                {
                    bool normal = false;
                    object curSumcnt = entry_special_rows[0]["trans_count"];
                    if (curSumcnt != DBNull.Value)
                    {
                        if (Convert.ToInt32(curSumcnt) >= dayMin && Convert.ToInt32(curSumcnt) <= dayMax)
                        {
                            normal = true;
                        }
                    }
                    if (!normal)
                    {
                        entry_special_anTable.ImportRow(entry_zh_rows[0]);
                    }
                }
                else
                {
                    DataRow addRow = entry_special_anTable.NewRow();
                    addRow["sum_date"] = anStartTime; addRow["trans_count"] = 0;
                    entry_special_anTable.Rows.Add(addRow);
                    //明细上也加上
                    DataRow addRsRow = resultSet.Tables[1].NewRow();
                    addRsRow["sum_date"] = anStartTime; addRsRow["trans_count"] = 0;
                    resultSet.Tables[1].Rows.Add(addRsRow);
                }
                //出口成功
                if (exit_zh_rows.Length > 0)
                {
                    bool normal = false;
                    object curSumcnt = exit_zh_rows[0]["trans_count"];
                    if (curSumcnt != DBNull.Value)
                    {
                        if (Convert.ToInt32(curSumcnt) >= dayMin && Convert.ToInt32(curSumcnt) <= dayMax)
                        {
                            normal = true;
                        }
                    }
                    if (!normal)
                    {
                        exit_zh_anTable.ImportRow(entry_zh_rows[0]);
                    }
                }
                else
                {
                    DataRow addRow = exit_zh_anTable.NewRow();
                    addRow["sum_date"] = anStartTime; addRow["trans_count"] = 0;
                    exit_zh_anTable.Rows.Add(addRow);
                    //明细上也加上
                    DataRow addRsRow = resultSet.Tables[2].NewRow();
                    addRsRow["sum_date"] = anStartTime; addRsRow["trans_count"] = 0;
                    resultSet.Tables[2].Rows.Add(addRsRow);
                }
                //出口失败
                if (exit_speical_rows.Length > 0)
                {
                    bool normal = false;
                    object curSumcnt = exit_speical_rows[0]["trans_count"];
                    if (curSumcnt != DBNull.Value)
                    {
                        if (Convert.ToInt32(curSumcnt) >= dayMin && Convert.ToInt32(curSumcnt) <= dayMax)
                        {
                            normal = true;
                        }
                    }
                    if (!normal)
                    {
                        exit_special_anTable.ImportRow(entry_zh_rows[0]);
                    }
                }
                else
                {
                    DataRow addRow = exit_special_anTable.NewRow();
                    addRow["sum_date"] = anStartTime; addRow["trans_count"] = 0;
                    exit_special_anTable.Rows.Add(addRow);
                    //明细上也加上
                    DataRow addRsRow = resultSet.Tables[3].NewRow();
                    addRsRow["sum_date"] = anStartTime; addRsRow["trans_count"] = 0;
                    resultSet.Tables[3].Rows.Add(addRsRow);
                }
                anStartTime = anStartTime.AddDays(1);
            }
            //分析绑定
            this.repeater_entry_zh_an.DataSource = entry_zh_anTable;
            this.repeater_entry_zh_an.DataBind();
            this.repeater_entry_special_an.DataSource = entry_special_anTable;
            this.repeater_entry_special_an.DataBind();
            this.repeater_exit_zh_an.DataSource = exit_zh_anTable;
            this.repeater_exit_zh_an.DataBind();
            this.repeater_exit_special_an.DataSource = exit_special_anTable;
            this.repeater_exit_special_an.DataBind();
            //明细绑定
            this.repeater_entry_zh.DataSource = resultSet.Tables[0];
            this.repeater_entry_zh.DataBind();
            this.repeater_entry_special.DataSource = resultSet.Tables[1];
            this.repeater_entry_special.DataBind();
            this.repeater_exit_zh.DataSource = resultSet.Tables[2];
            this.repeater_exit_zh.DataBind();
            this.repeater_exit_special.DataSource = resultSet.Tables[3];
            this.repeater_exit_special.DataBind();
        }
        int dayMin = 100; int dayMax = 2000000;
        public string GetStatCount(object sumCnt)
        {
            StringBuilder sbText = new StringBuilder();
            if (sumCnt != DBNull.Value)
            {
                if (Convert.ToInt64(sumCnt) < dayMin||Convert.ToInt32(sumCnt)>dayMax)
                {
                    sbText.Append("<font color='red'>" + sumCnt.ToString() + "</font>");
                }
                else
                {
                    sbText.Append(sumCnt.ToString());
                }
            }
            return sbText.ToString();
        }
    }
}